$(document).ready(function(){
	init();
});

function init(){
	window.tabId = "#startUpMenuOuter"; //if you change this variable change the css file for startUpMenu as well
	window.professionId = ":radio";
	
	// global vars
	window.check = $('.checkbox');
	window.errorMark = $('.errorMark');
	window.errorString='<div id="error">';
	window.namePattern = /^('?([a-z]+|[A-Z]+)+(('|-)([a-z]|[A-Z]))?\s?)+$/i;
		
	setUpDefault(tabId,professionId);
	keyListener();
	
	$('[name=profession]').first().click();
	$('#id_startDate').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        onClose: function(dateText, inst) { 
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            $(this).datepicker('setDate', new Date(year, month, 1));
        }
    });
}

function setUpDefault(tabId,professionId)
{
	$(tabId).tabs();
	$(':button').button();	
}

function validate(value) 
{	
	var valid=false;
	// Capital letter, any number of lowercase optional space another capital letter
	
	if (value.match(namePattern) != null)  //maybe != ""
	{
		valid = true;
	}
	
	return valid;
}

function keyListener()
{
	$(':text').not('#id_startDate').keypress(function(event) {
		$this = jQuery(event.target);
		if (validate($this.val())) {
			$this.next('.image').remove();
			$this.after(check.clone());
		}
		else {
			$this.next('.image').remove();
			$this.after(errorMark.clone());
		}
	});
	
	$(':text').not('#id_startDate').keyup(function(event) {
		$this = jQuery(event.target);
		if (validate($this.val())) {
			$this.next('.image').remove();
			$this.after(check.clone());
		}
		else {
			$this.next('.image').remove();
			$this.after(errorMark.clone());
		}
	}); 
}

	
function professionHelper() {
	var tmpTip;
	if (radioObject.checked)
	{
		document.write(tmpTip); // tmpTip will be stored somewhere else for
	}						// improved abstraction
}

function submitTeamInfo($this){
	teamInfoID = '#teamInfoForm';
	$this = jQuery($this);
	$.ajax({url:$this.attr('url'),
		type: "POST",
		data: $this.parents('form').serialize(),
		dataType: 'html',
		success:function(info){
			
			if (info.indexOf(errorString) != -1){
				$.notifyBar({
					html: 'Your form has errors, please correct them below',
					delay: 2000,
					animationSpeed: "normal",
					cls: "error"
				});
				$(teamInfoID+' *').remove();
				$(teamInfoID).append(info);
				init();
			} 
			else {
//				$(teamInfoID+' *').remove();
//				$(teamInfoID).append(info);
//				$(teamInfoID+' :input').attr('disabled','true');
//				$(teamInfoID+' :button').remove();
//				$(tabId).tabs('destroy')
				window.location = info;
				
			}
		},
		error:function(){
			$.notifyBar({
				html: 'An error occured while sending the data',
				delay: 2000,
				animationSpeed: "normal",
				cls: "error"
			});
		}
	});
}